#include <sys_config.h>

#include <types.h>
#include <osal/osal.h>
#include <basic_types.h>
#include <mediatypes.h>
#include <api/libc/printf.h>
#include <api/libc/string.h>
#include <osal/osal.h>
#include <hld/hld_dev.h>

#include <api/libosd/osd_lib.h>

#include "osdobjs_def.h"

#include "string.id"
#include "images.id"
#include "osd_config.h"
#include "osd_rsc.h"

#include "copper_common/system_data.h"
#include "copper_common/menu_api.h"
#include "win_com_menu_define.h"
#include "win_com.h"

#include <hld/dmx/dmx_dev.h>
#include <hld/dmx/dmx.h>
#include <hld/decv/vdec_driver.h>
#ifndef NEW_DEMO_FRAME
#include <api/libpub29/lib_hde.h>
#include <api/libpub29/lib_pub27.h>
#else
#include <api/libpub/lib_hde.h>
#include <api/libpub/lib_pub.h>
#endif

#include "menus_root.h"

#ifdef _LCN_ENABLE_
//#ifndef DVBS_SUPPORT
/*******************************************************************************
*	Objects definition
*******************************************************************************/
extern CONTAINER g_win_LCN;

extern CONTAINER LCN_item_con1;
extern TEXT_FIELD LCN_item_txtname1;
extern TEXT_FIELD LCN_item_txtset1;


static VACTION LCN_item_con_keymap(POBJECT_HEAD pObj, UINT32 key);
static PRESULT LCN_item_con_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2);

static VACTION LCN_con_keymap(POBJECT_HEAD pObj, UINT32 key);
static PRESULT LCN_con_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2);
static BOOL current_frontend_is_isdbt=FALSE;
#if Maker_GUI_ON
#ifndef SUPPORT_CAS_A
/* define in win_com_menu_define.h already */
#define	W_L     	Maker_GUI_SM_L
#define	W_T     	Maker_GUI_SM_T
#define	W_W     	Maker_GUI_SM_W
#define	W_H     	Maker_GUI_SM_H
#endif
#else
#ifndef SUPPORT_CAS_A
/* define in win_com_menu_define.h already */
#define	W_L     	248
#define	W_T     	98
#define	W_W     	692
#define	W_H     	488
#endif
#endif

#define CON_L		(W_L+20)
#define CON_T		(W_T + 4)
#define CON_W		(W_W - 60)
#define CON_H		40
#define CON_GAP		12

#define TXT_L_OF  	10
#define TXT_W  		300
#define TXT_H		CON_H
#define TXT_T_OF	((CON_H - TXT_H)/2)

#define SEL_L_OF  	(TXT_L_OF + TXT_W)
#define SEL_W  		(CON_W-320)
#define SEL_H		CON_H
#define SEL_T_OF	((CON_H - SEL_H)/2)

#define LINE_L_OF	0
#define LINE_T_OF  	(CON_H+4)
#define LINE_W		CON_W
#define LINE_H     	4


#define LDEF_CON(root, varCon,nxtObj,ID,IDl,IDr,IDu,IDd,l,t,w,h,conobj,focusID)		\
    DEF_CONTAINER(varCon,root,nxtObj,C_ATTR_ACTIVE,0, \
    ID,IDl,IDr,IDu,IDd, l,t,w,h, CON_SH_IDX,CON_HL_IDX,CON_SL_IDX,CON_GRY_IDX,   \
    LCN_item_con_keymap,LCN_item_con_callback,  \
    conobj, focusID,1)

#define LDEF_TXTNAME(root,varTxt,nxtObj,ID,IDl,IDr,IDu,IDd,l,t,w,h,resID,str)		\
    DEF_TEXTFIELD(varTxt,root,nxtObj,C_ATTR_ACTIVE,0, \
    ID,IDl,IDr,IDu,IDd, l,t,w,h, TXTN_SH_IDX,TXTN_HL_IDX,TXTN_SL_IDX,TXTN_GRY_IDX,   \
    NULL,NULL,  \
    C_ALIGN_LEFT | C_ALIGN_VCENTER, 0,0,resID,str)

#define LDEF_TXTSET(root,varTxt,nxtObj,ID,IDl,IDr,IDu,IDd,l,t,w,h,resID,str)		\
    DEF_TEXTFIELD(varTxt,root,nxtObj,C_ATTR_ACTIVE,0, \
    ID,IDl,IDr,IDu,IDd, l,t,w,h, TXTS_SH_IDX,TXTS_HL_IDX,TXTS_SL_IDX,TXTS_GRY_IDX,   \
    NULL,NULL,  \
    C_ALIGN_CENTER| C_ALIGN_VCENTER, 20,0,resID,str)


#define LDEF_MM_ITEM(root,varCon,nxtObj,varTxtName,varTxtset,ID,IDu,IDd,l,t,w,h,nameID,setstr)	\
	LDEF_CON(&root,varCon,nxtObj,ID,ID,ID,IDu,IDd,l,t,w,h,&varTxtName,1)	\
	LDEF_TXTNAME(&varCon,varTxtName,&varTxtset,0,0,0,0,0,l + TXTN_L_OF, t + TXTN_T_OF,TXTN_W,TXTN_H,nameID,NULL)	\
	LDEF_TXTSET(&varCon,varTxtset,  NULL        ,1,1,1,1,1,l + TXTS_L_OF, t + TXTS_T_OF,TXTS_W,TXTS_H,0,setstr)

LDEF_MM_ITEM(g_win_LCN,LCN_item_con1, NULL,LCN_item_txtname1,LCN_item_txtset1,1,1,1,	\
				CON_L, CON_T + (CON_H + CON_GAP)*0,CON_W,CON_H,	RS_INSTALLATION_LCN,NULL)

DEF_CONTAINER(g_win_LCN,NULL,NULL,C_ATTR_ACTIVE,0, \
    1,0,0,0,0, W_L,W_T,W_W,W_H, WIN_SH_IDX,WIN_HL_IDX,WIN_SL_IDX,WIN_GRY_IDX,   \
    LCN_con_keymap,LCN_con_callback,  \
    (POBJECT_HEAD)&LCN_item_con1, 1,0)


/*******************************************************************************
*	Local functions & variables define
*******************************************************************************/
CONTAINER* LCN_items[] =
{
	&LCN_item_con1,
};

POBJECT_HEAD LCN_item_name[] =
{
	(POBJECT_HEAD)&LCN_item_txtname1,
};

POBJECT_HEAD LCN_item_set[] =
{
	(POBJECT_HEAD)&LCN_item_txtset1,
};


void win_LCN_set_string(UINT8 input)
{
	if(input==0)
		OSD_SetTextFieldContent(&LCN_item_txtset1, STRING_ID,RS_COMMON_OFF);				
	else
		OSD_SetTextFieldContent(&LCN_item_txtset1, STRING_ID,RS_COMMON_ON);	
}

/*******************************************************************************
*	key mapping and event callback definition
*******************************************************************************/

static VACTION LCN_item_con_keymap(POBJECT_HEAD pObj, UINT32 key)
{
	VACTION act;
	
	switch(key)
	{
	case V_KEY_RIGHT:
		act = VACT_INCREASE;
		break;		
	case V_KEY_LEFT:		
		act = VACT_DECREASE;
		break;		
	case V_KEY_ENTER:
		act = VACT_ENTER;
		break;		
	default:	
		act = VACT_PASS;
		break;
	}

	return act;

}


PRESULT comlist_menu_LCN_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
	UINT16 i,wTop;
	UINT8 id;
	char str_len;
    	OBJLIST* ol;
	UINT8 temp[4];
	
	PRESULT cbRet = PROC_PASS;

    	ol = (OBJLIST*)pObj;
	
	if(event==EVN_PRE_DRAW)
	{
	}
	else if(event == EVN_POST_CHANGE)
		cbRet = PROC_LEAVE;

	return cbRet;
}


void win_LCN_left_right_key(POBJECT_HEAD pObj,UINT8 id)
{
	sys_data_set_LCN(!sys_data_get_LCN());
	win_LCN_set_string(sys_data_get_LCN());
	OSD_TrackObject(pObj,C_UPDATE_ALL);
}

static PRESULT LCN_item_con_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
	PRESULT ret = PROC_PASS;
	UINT8	unact,input;
	UINT8	id = OSD_GetObjID(pObj);

	switch(event)
	{		
	case EVN_FOCUS_PRE_GET:
		break;
		
	case EVN_FOCUS_PRE_LOSE:
		break;

	case EVN_UNKNOWN_ACTION:
		unact = (VACTION)(param1>>16) ;
		if((unact == VACT_INCREASE) || (unact == VACT_DECREASE))
		{
			win_LCN_left_right_key(pObj,id);
		}
        if(sys_data_get_LCN())
			sort_prog_node(PROG_LCN_SORT);
		else
			sort_prog_node(PROG_DEFAULT_SORT);
		update_data();
		ret = PROC_LOOP;
		break;
	}
	return ret;

}

static VACTION LCN_con_keymap(POBJECT_HEAD pObj, UINT32 key)
{
	VACTION act;
	
	switch(key)
	{
	case V_KEY_EXIT:
	case V_KEY_MENU:
		act = VACT_CLOSE;
		break;
	default:	
		act = VACT_PASS;
		break;
	}

	return act;
}

static PRESULT LCN_con_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
	PRESULT ret = PROC_PASS;
	UINT8	unact;
	UINT32 i,j;
	BOOL subt_flag;
	CONTAINER* cont = (CONTAINER*)pObj;
	UINT8	id = OSD_GetObjID(pObj);
	UINT16 title_id;
	UINT8 group_type,  group_pos;
	UINT16 channel,ch_cnt;
	UINT8 av_flag;
	static P_NODE p_node1;
	P_NODE p_node2;
	UINT8	tmp_flag = 1;
	switch(event)
	{
	case EVN_PRE_OPEN:
		if(param2 != MENU_OPEN_TYPE_STACK)
        {
            if((param2 & MENU_FOR_ISDBT) == MENU_FOR_ISDBT)
                current_frontend_is_isdbt = TRUE;           
            else
                current_frontend_is_isdbt = FALSE;
        }
        if(current_frontend_is_isdbt)
        	title_id = RS_INSTALLATION_VCN;
		else 
     		title_id = RS_INSTALLATION_LCN;
		wincom_open_title((POBJECT_HEAD)&g_win_LCN,title_id, 0);
		OSD_SetTextFieldContent(&LCN_item_txtname1, STRING_ID, title_id);
		
		win_LCN_set_string(sys_data_get_LCN());
		OSD_DrawObject((POBJECT_HEAD )&LCN_item_con1,C_UPDATE_ALL);
		sys_data_get_curprog_info(&p_node1);
		break;
	
	case EVN_POST_OPEN:
		break;
		
	case EVN_PRE_CLOSE:
		sys_data_save(1);
		/* Make OSD not flickering */
		*((UINT32*)param2) &= ~C_CLOSE_CLRBACK_FLG;	
		break;

	case EVN_POST_CLOSE:
		sys_data_get_cur_mode_group_infor(sys_data_get_cur_group_index(),&group_type, &group_pos, &channel);
		av_flag = sys_data_get_cur_chan_mode();
		ch_cnt = get_prog_num(VIEW_ALL | av_flag, 0);
		
		for( i=0; i<ch_cnt; i++ )
		{
			get_prog_at( i, &p_node2 );
			for( j=0; j<2*(MAX_SERVICE_NAME_LENGTH + 1); j++ )
			{
				if( p_node1.service_name[j] != p_node2.service_name[j])
				{
					tmp_flag = 0;
					break;
				}
			}
			if(tmp_flag)
			{
				sys_data_set_cur_group_channel(i);
				break;
			}
			else
			{
				tmp_flag = 1;
			}
		}
		break;
	}
	return ret;
}

//#endif
#endif
